//
//  HiChatInfoCell.m
//  HiCar
//
//  Created by huachun on 2019/3/22.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "HiChatInfoCell.h"
#import "ChatModel.h"
#import "OSSTestMacros.h"
#import "HiJonesClass.h"
#import "XLPhotoBrowser.h"

@interface HiChatInfoCell ()

@end

@implementation HiChatInfoCell

+ (instancetype)cellForTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"HiChatInfoCell";
    
    HiChatInfoCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell=[[HiChatInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor=[UIColor clearColor];
        [tableView setSeparatorColor:[UIColor clearColor]];
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
        tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        tableView.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);
        
        [cell.contentView addSubview:cell.User_logo];
        [cell.contentView addSubview:cell.tiBG];
        [cell.contentView addSubview:cell.Title];
        [cell.contentView addSubview:cell.date];
        [cell.contentView addSubview:cell.user_go];
        [cell.contentView addSubview:cell.zhuangtai];
        
        [cell.contentView addSubview:cell.Chat_logo];
        [cell.contentView addSubview:cell.chat_logo_button];
    }
    
    return cell;
}

- (void)setModel:(ChatModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    
    _date.text=[HiJonesClass timeZhuan:_Model.Chat_date];
    
    NSMutableAttributedString *attributedStringT = [[NSMutableAttributedString alloc] initWithString:_Model.Chat_info];
    NSMutableParagraphStyle *paragraphStyleT = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyleT setLineSpacing:3];
    [attributedStringT addAttribute:NSParagraphStyleAttributeName value:paragraphStyleT range:NSMakeRange(0, [_Model.Chat_info length])];
    
    [attributedStringT addAttribute:NSFontAttributeName
                              value:[UIFont systemFontOfSize:15]
                              range:NSMakeRange(0, attributedStringT.length)];
    
    CGRect rect = [attributedStringT boundingRectWithSize:CGSizeMake(RECRWITCH/5*3, 10000)
                                                  options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                  context:nil];
    _Title.attributedText = attributedStringT;
    
    
    _Title.hidden = NO;
    _tiBG.hidden = NO;
    
    _Chat_logo.hidden = YES;
    _chat_logo_button.hidden = YES;
    
    if ([_Model.Chat_type intValue]==6 || [_Model.Chat_type intValue]==8 || [_Model.Chat_type intValue]==9 || [_Model.Chat_type intValue]==10|| [_Model.Chat_type intValue]==11) {
        _user_go.hidden=NO;
        _zhuangtai.hidden=YES;
        if (rect.size.height>25) {
            _Title.frame=CGRectMake(80, 20, RECRWITCH/5*3, rect.size.height+5);
            _tiBG.frame=CGRectMake(75, 15, RECRWITCH/5*3+10, rect.size.height+15);
        }else{
            _Title.frame=CGRectMake(80, 20, rect.size.width+5, rect.size.height+5);
            _tiBG.frame=CGRectMake(75, 15, rect.size.width+15, rect.size.height+15);
        }
        
        [_Title setTextAlignment:NSTextAlignmentLeft];
        _date.frame=CGRectMake(80, 20+rect.size.height+10, 150, 20);
        _date.textAlignment=NSTextAlignmentLeft;
        
        _User_logo.frame = CGRectMake(15, 10, 50, 50);
        NSString *imgurl = _Model.User_logo;
        [_User_logo sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"iconlogin.png"]];
        
        if ([_Model.Chat_type intValue]==8) {
            _Title.hidden = YES;
            _tiBG.hidden = YES;
            
            _Chat_logo.hidden = NO;
            _chat_logo_button.hidden = NO;
            
            _Chat_logo.frame=CGRectMake(75, 15, 140 + 15, 140 + 15);
            _chat_logo_button.frame = _Chat_logo.frame;
            _Chat_logo.image = [UIImage imageNamed:@"loading.gif"];
            
            NSString *imgurl = [NSString stringWithFormat:@"%@/%@",OSS_IMG_ENDPOINT,_Model.Chat_info];
            [_Chat_logo sd_setImageWithURL:[NSURL URLWithString:imgurl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if (error) {
                    self->_Chat_logo.image = [UIImage imageNamed:@"chat_shibai.png"];
                }
            }];
            
            _date.frame=CGRectMake(80, 20+140+10, 150, 20);
        }else if ([_Model.Chat_type isEqualToString:@"10"]||[_Model.Chat_type isEqualToString:@"11"]){
            _Title.font=[UIFont systemFontOfSize:17];
            _Title.frame=CGRectMake(80, 20, RECRWITCH/5*3, 100);
        }
        
    }else{
        _user_go.hidden=YES;
        _zhuangtai.hidden=NO;
        if (rect.size.height>25) {
            _Title.frame=CGRectMake(RECRWITCH-RECRWITCH/5*3-80, 20, RECRWITCH/5*3, rect.size.height+5);
            _tiBG.frame=CGRectMake(RECRWITCH-RECRWITCH/5*3-85, 15, RECRWITCH/5*3+10, rect.size.height+15);
        }else{
            _Title.frame=CGRectMake(RECRWITCH-rect.size.width-5-80, 20, rect.size.width+5, rect.size.height+5);
            _tiBG.frame=CGRectMake(RECRWITCH-rect.size.width-5-85, 15, rect.size.width+15, rect.size.height+15);
            _zhuangtai.frame=CGRectMake(RECRWITCH-150-80, 20+rect.size.height+10, 150, 20);
        }
        _date.frame=CGRectMake(RECRWITCH-150-80, 20+rect.size.height+10, 150, 20);
        _date.textAlignment=NSTextAlignmentRight;
        
        _zhuangtai.frame=CGRectMake(RECRWITCH/2-50, 20+rect.size.height+12.5, 100, 15);
        _zhuangtai.textColor=[JonesClass stringTOColor:@"#999999"];
        if ([_Model.Send_type intValue]==2) {
            _zhuangtai.text=@"发送中";
        }else if ([_Model.Send_type intValue]==3) {
            _zhuangtai.text=@"发送失败";
            _zhuangtai.textColor=[UIColor redColor];
        }else if ([_Model.Send_type intValue]==4) {
            _zhuangtai.text=@"对方拒绝陌生人";
        }else if ([_Model.Send_type intValue]==5) {
            _zhuangtai.text=@"对方已将你拉入黑名单";
        }else if ([_Model.Send_type intValue]==1) {
            _zhuangtai.text=@"发送成功";
        }else{
            _zhuangtai.text=@"";
        }
        
        _User_logo.frame = CGRectMake(RECRWITCH-65, 10, 50, 50);
        NSString *imgurl = [UserModel info].logo;
        [_User_logo sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"iconlogin.png"]];
        
        if ([_Model.Chat_type intValue]==18) {
            _Title.hidden = YES;
            _tiBG.hidden = YES;
            
            _Chat_logo.hidden = NO;
            _chat_logo_button.hidden = NO;
            
            _Chat_logo.frame=CGRectMake(RECRWITCH-140-5-85, 15, 140 + 15, 140 + 15);
            _chat_logo_button.frame = _Chat_logo.frame;
            _Chat_logo.image = [UIImage imageNamed:@"loading.gif"];
            
            NSString *imgurl = [NSString stringWithFormat:@"%@/%@",OSS_IMG_ENDPOINT,_Model.Chat_info];
            [_Chat_logo sd_setImageWithURL:[NSURL URLWithString:imgurl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if (error) {
                    self->_Chat_logo.image = [UIImage imageNamed:@"chat_shibai.png"];
                }
            }];
            
            _date.frame=CGRectMake(RECRWITCH-150-80, 20+140+10, 150, 20);
            
            _zhuangtai.frame=CGRectMake(RECRWITCH/2-50, 20+140+12.5, 100, 15);
        }else if ([_Model.Chat_type isEqualToString:@"20"]){
            _Title.font=[UIFont systemFontOfSize:17];
            _Title.frame=CGRectMake(RECRWITCH-RECRWITCH/5*3-80, 20, RECRWITCH/5*3, 50);
            if ([_Model.Send_type isEqualToString:@"1"]) {
                _Title.text = @"您已同意TA参加";
            }else if ([_Model.Send_type isEqualToString:@"21"]){
                _Title.text = @"您已拒绝TA参加";
            }
        }
    }
}

-(UIImageView *)User_logo{
    if (!_User_logo) {
        _User_logo       = [[UIImageView alloc] init];
        _User_logo.layer.masksToBounds=YES;
        _User_logo.layer.cornerRadius=25;
        _User_logo.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _User_logo;
}

-(UIImageView *)Chat_logo{
    if (!_Chat_logo) {
        _Chat_logo       = [[UIImageView alloc] init];
        _Chat_logo.layer.masksToBounds=YES;
        _Chat_logo.layer.cornerRadius=8;
        _Chat_logo.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _Chat_logo;
}

-(UIButton *)chat_logo_button{
    if (!_chat_logo_button) {
        _chat_logo_button =[[UIButton alloc] init];
        _chat_logo_button.frame=CGRectMake(15, 10, 50, 50);
        [_chat_logo_button addTarget:self action:@selector(look_chatimg) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chat_logo_button;
}

-(UIButton *)user_go{
    if (!_user_go) {
        _user_go =[[UIButton alloc] init];
        _user_go.frame=CGRectMake(15, 10, 50, 50);
        [_user_go addTarget:self action:@selector(rootuserlook) forControlEvents:UIControlEventTouchUpInside];
    }
    return _user_go;
}

-(void)rootuserlook{
    self.selectblock(1);
}

- (void)look_chatimg{
    NSMutableArray *ling_img_array=[NSMutableArray new];
    NSString *imgurl = [NSString stringWithFormat:@"%@/%@",OSS_IMG_ENDPOINT,_Model.Chat_info];
    [ling_img_array addObject:imgurl];
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:ling_img_array currentImageIndex:0];
    browser.seletBlock = ^(BOOL tui_ok) {
        
    };
}

-(UIView *)tiBG{
    if (!_tiBG) {
        _tiBG=[[UIView alloc] init];
        _tiBG.backgroundColor=[UIColor whiteColor];
        _tiBG.layer.borderColor=[JonesClass stringTOColor:@"#f0f0f0"].CGColor;
        _tiBG.layer.borderWidth=0.5;
        _tiBG.layer.cornerRadius=4.0;
    }
    return _tiBG;
}

-(UILabel *)Title{
    if (!_Title) {
        _Title = [[UILabel alloc] init];
        _Title.font=[UIFont systemFontOfSize:13];
        _Title.numberOfLines=0;
        [_Title setTextAlignment:NSTextAlignmentLeft];
        [_Title setTextColor:[JonesClass stringTOColor:@"#000000"]];
    }
    return _Title;
}

-(UILabel *)date{
    if (!_date) {
        _date = [[UILabel alloc] init];
        _date.font=[UIFont systemFontOfSize:11];
        [_date setTextColor:[JonesClass stringTOColor:@"#999999"]];
    }
    return _date;
}

-(UILabel *)zhuangtai{
    if (!_zhuangtai) {
        _zhuangtai = [[UILabel alloc] init];
        _zhuangtai.font=[UIFont systemFontOfSize:11];
        _zhuangtai.textAlignment=NSTextAlignmentCenter;
        [_zhuangtai setTextColor:[UIColor redColor]];
    }
    return _zhuangtai;
}

@end
